/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.service;

import java.util.List;

import org.hibernate.Session;

import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.service.BaseService;
import etcore.webapp.grouptalk.user.dao.MemberDAO;
import etcore.webapp.grouptalk.user.object.Member;

/**
 * MemberService implements queries and transactions on Member
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class MemberService extends BaseService {

	public Member findByUsernameAndGid(String u, Integer groupid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			Member result = dao.findByUsernameAndGid(u, groupid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public String getSystemAdmins(int begin,int count) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			String result = dao.getSystemAdmins(begin,count);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public String getAllSystemAdmins(int begin,int count) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			String result = dao.getAllSystemAdmins(begin,count);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public Member findByUsernameAndTid(String u, Integer topicid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			Member result = dao.findByUsernameAndTid(u, topicid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public Member findByUsernameAndGmid(String u, Integer messageid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			Member result = dao.findByUsernameAndGmid(u, messageid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public Member findByUsernameAndGcid(String u, Integer commentid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			Member result = dao.findByUsernameAndGcid(u, commentid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public boolean isAdmin(String userName, Integer gid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			boolean result = dao.isAdmin(userName, gid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public boolean isMember(String userName, Integer gid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			boolean result = dao.isMember(userName, gid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public boolean isOwner(String userName, Integer gid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			boolean result = dao.isOwner(userName, gid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long energySum(String u) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.energySum(u);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long getSysAdminNum()  {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.getSysAdminNum();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long getAllSysAdminNum()  {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.getAllSysAdminNum();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long attendGroupNum(String u, String mytype) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.attendGroupNum(u, mytype);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long isIn(String u, Integer groupid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.isIn(u, groupid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public boolean isSysAdmin(String userName) {

		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			boolean result = dao.isSysAdmin(userName);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Member> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Member> result = dao.findByProperty(propertyName, value,
					begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Member> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Member> result = dao.findByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Member> findMember(Integer gid, String type, Integer lock) {

		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Member> result = dao.findMember(gid, type, lock);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Member> findMember(Integer gid, String type, Integer lock,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Member> result = dao
					.findMember(gid, type, lock, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long getMemberNum(Integer gid, String type, Integer lock) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.getMemberNum(gid, type, lock);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long getAllMemberNum(Integer gid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.getAllMemberNum(gid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long getLockedMemberNum(Integer gid) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.getLockedMemberNum(gid);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long findSizeOfAdminFollowing(Integer open) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.findSizeOfAdminFollowing(open);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long findSizeOfAdminNotFollowing(Integer open,String fromUser) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.findSizeOfAdminNotFollowing(open,fromUser);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public List<Integer>   findGroupsOfAdminNotFollowing(Integer open,String fromUser,int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Integer> result = dao.findGroupsOfAdminNotFollowing(open,fromUser,begin,length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long findSizeOfAdminFollowing(Integer open,String inputDate) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.findSizeOfAdminFollowing(open,inputDate);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long findAdminsSizeByMaxHisFollowed(Integer open,long  maxFollowed) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			long result = dao.findAdminsSizeByMaxHisFollowed(open,maxFollowed);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public List<Object[]> findAdminsByMaxHisFollowed(Integer open,long  maxFollowed,int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Object[]> result = dao.findAdminsByMaxHisFollowed(open,maxFollowed, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public List<Member> getAllMember(Integer gid, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Member> result = dao.getAllMember(gid, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public List<Member> getLockedMember(Integer gid, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Member> result = dao.getLockedMember(gid, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	@SuppressWarnings("rawtypes")
	public List findMemberGroups(Integer open, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List result = dao.findMemberGroups(open, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	@SuppressWarnings("rawtypes")
	public List findEnglishMemberGroups(Integer open, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List result = dao.findEnglishMemberGroups(open, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	@SuppressWarnings("rawtypes")
	public List findLocalMemberGroups(Integer open, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List result = dao.findLocalMemberGroups(open, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	@SuppressWarnings("rawtypes")
	public List findGroupStars(Integer open, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List result = dao.findGroupStars(open, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	@SuppressWarnings("rawtypes")
	public List findEnglishGroupStars(Integer open, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List result = dao.findEnglishGroupStars(open, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	@SuppressWarnings("rawtypes")
	public List findLocalGroupStars(Integer open, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List result = dao.findLocalGroupStars(open, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Member> findAll() {
		Session session = HibernateSessionFactory.getSession();
		try {
			MemberDAO dao = new MemberDAO();
			List<Member> result = dao.findAll();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

}